package com.jiazhong.spring.aop.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class UserAdvice {

    @Pointcut("execution(* com.jiazhong.spring.aop.target.*.save(..))")
    private void execute() {
    }

    /* 不获取目标对象的参数
    @AfterReturning("execution(* com.jiazhong.spring.aop.target.*.*(..))")
    public void transactionCommit(){
        System.out.println("执行了事务提交");
    }*/
    //
    @AfterReturning("execute()")
    public void transactionCommit(JoinPoint joinPoint) {
        // 获取到目标对象的参数
        Object[] args = joinPoint.getArgs();
        System.out.println("执行了事务提交,姓名是：" + Arrays.toString(args));
    }

    @Before("execute()")
    public void before() {
        System.out.println("用户开始执行操作");
    }

    @Around("execute()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕之前.......");
        Object result = joinPoint.proceed();
        System.out.println("返回结果：" + result);
        System.out.println("环绕之后.......");
    }

    @After("execute()")
    public void after() {
        System.out.println("我是最终的....");
    }

    @AfterThrowing("execute()")
    public void exception() {
        System.out.println("你的代码出现了异常情况");
    }
}
